/**
 * Filename: d:\CODE\FTP\FTP_TEST\FTP\static\js\register.js
 * Path: d:\CODE\FTP\FTP_TEST\FTP\static\js
 * Created Date: Tuesday, May 10th 2022, 11:46:17 am
 * Author: xiguan
 * 
 * Copyright (c) 2022 XiGuan Inc.
 */


 const basic_url = "http://127.0.0.1:8880";

 // 封装发送请求
 function ajax(params){
     let url = basic_url+params.url;
     let data = params.data;
     let method = "GET";
     if(params.method !== ""){
         method = params.method;
     }
 
     return $.ajax({
         url : url,
         data : data,
         method: method
     });
}

// 监听
$(function(){
    // 用户名标志
    let username_flag = false;
    // 密码标志
    let password_flag = false;
    // 验证码标志
    let chapter_flag = false;
    // 验证码
    let chapter = "";
    // 验证码生成时间戳
    let timestamp = 0;

    // 邮箱输入框失去焦点，判断邮箱格式
    $("#username").blur(async() => {
        let username = $("#username").val();
        username_flag = false;
        let tip = $("#tip");
        if(username === ""){
            $('#tip').text("提示: 邮箱不能为空!");
            $("#tip").show();
            setTimeout(() => {
                $("#tip").hide();
            }, 1500);
            // alert("提示: 邮箱不能为空!");
        }
        else{
            let judge_str = /^[\w\-.]+@[\w\-.]+(\.\w+)+$/;
            if(!judge_str.test(username)){
                // $('#tip').text("提示：邮箱格式错误");
                // $("#tip").show();
                alert("提示：邮箱格式错误!");
            }
            else{
                let params = {
                    url : "/user/is_exist",
                    data : {
                        email : username
                    }
                };
                tip = $("#tip");
                await ajax(params).then(data => {
                    console.info(data,"hhaha");
                    if (data === "True") {
                        // tip.text("提示：邮箱已经注册");
                        // tip.show();
                        alert("提示：邮箱已经注册!");
                    } else {
                        username_flag = true;
                    }
                });
            }
        }
        if(username_flag && password_flag){
            $("#chapter-btn").attr("disabled", false);
        }
        else{
            $("#chapter-btn").attr("disabled", true);
        }
    });

    // 密码输入框失去焦点，判断密码格式
    $("#password").blur(() => {
        let password = $("#password").val();
        password_flag = false;
        if(password === "") {
            $('#tip').text("提示：密码不能为空");
            $("#tip").show();
            setTimeout(() => {
                $("#tip").hide();
            }, 1500);
            // alert("提示：密码不能为空!")
        } else {
            password_flag = true;
        }
        // 标志
        if(username_flag && password_flag) {
            $("#chapter-btn").attr("disabled", false);
        } else {
            $("#chapter-btn").attr("disabled", true);
        }
    });


    // 点击获取验证码
    $("#chapter-btn").click(() => {
        // $("#chapter-btn").attr("disabled", true);
        console.log(1)
        let params = {
            url: "/user/get_vercode",
            data: {
                username: $("#username").val()
            }
        };
        ajax(params).then(res => {
            if(res.status === "OK") {
                chapter = res.chapter;
                timestamp = res.timestamp;
                chapter_flag = true;
            }
        });
        $("#chapter-btn").attr("disabled", true);
        let time = 30;
        let interval_id =  setInterval(() => {
            if(time < 10) {
                $('#chapter-btn').text("0" + time + "秒");
            } else {
                $('#chapter-btn').text(time + "秒");
            }

            time --;
            if(time < 0) {
                $("#chapter-btn").text("获 取");
                $("#chapter-btn").attr("disabled", false);
                clearInterval(interval_id);
            }
        }, 1000);
    });

    // 点击去登录
    $("#to-login").click(() => {
        // 跳转
        window.location.href="/user/go_login";
    });

    // // 点击立即登录
    // $("#login-now").click(() => {
    //     // 跳转
    //     window.location.href="/user/go_login";
    // });

    // 点击注册
    $("#register-btn").click(() => {
        if($("#chapter").val() !== "") {
            chapter_flag = true;
        }
       if(!username_flag || !password_flag || !chapter_flag) {
            $('#tip').text("提示：请完善信息...");
            $("#tip").show();
            setTimeout(() => {
                $("#tip").hide();
            }, 1500);
       } else {
           let current_timestamp = new Date().getTime();
           let time = current_timestamp/1000 - timestamp;
           console.info("time:", time, ":123:", timestamp);
        //    time = 25;
           if(time >= 60) {
                $('#tip').text("提示：验证码失效...");
                $("#tip").show();
                setTimeout(() => {
                  $("#tip").hide();
                }, 1500);
           } else if(chapter !== $("#chapter").val()) {
                $('#tip').text("提示：验证码错误...");
                $("#tip").show();
                setTimeout(() => {
                  $("#tip").hide();
                }, 1500);
           } else {
               let params = {
                   url: "/user/register_do",
                   data: {
                       email: $("#username").val(),
                       password: $("#password").val()
                   },
                   method: "POST"
               };
               ajax(params).then(data => {
                    if(data === "OK") {
                        alert("注册成功!")
                        // register_container.hide();
                        // login_now_tip.show();
                        // login_now.show();
                        window.location.href("/user/go_login");
                    }
                });
           }
       }
    });

})

//1361086019@qq.com